As part of electron session module, we're provided a full API for working with cookies, cookies,
of course, being a Web based data store, hence it being part of an electron session on a Web content
instance.
So with that said, step one is going to be requiring the session module from electron and as in the
previous lesson, get a reference to the default session on which will be storing cookie data, let
sess or session equal session dot default session on which we can then use the cookies, class session
dot cookies, get the standard guitar method for reading session cookies.
This takes one argument being an object with properties to filter cookies by which I leave empty, meaning
just get all cookies.
So no filter.
Then to read the cookies, this function returns a promise so resolved that return promise directly
on the function with then in which we can read the cookies as the first argument.
Which are logout.
Also, as with any promise, errors can now be accessed by means of a catch function.
Check the terminal and of course, we have an empty array as no cookies have been set before we set
our own cookie.
However, remember that this class is used for managing all cookies, even though set by remote content.
Before I demonstrate, I'll turn this guitar into a function just to make it reusable.
Let get cookies equal a function moving that get method with its message into the function body.
Then to load some remote content that sets cookies, comment out loud file replacing it with main window
dot jurel loading https github dot com.
Of course to read the cookies GitHub sets on the default session, we'll have to wait for the content
to fully load.
So using an event we've already seen listen on main window dot web content for did finish load with
a callback in which we can check for cookies as before by calling get cookies.
To recap, we load the content from this you well into my new Windows Web contents.
This remote content will then create a few cookies on the default session used by main Windows Web contents,
and after it's completely downloading, we get all cookies on the default session and log them out.
Save.
We see electron complaining about insecure JavaScript here, this just being due to the project content
security policy, check the terminal and there we have several cookies on our default session.
For example, underscore Geet being Google Analytics on GitHub dot com.
So let's add our own cookie, then I'll change back to loading indexed HTML.
And assigned to Kookie an object with its first property being neutral https my app domain dot com,
then the name of the cookie we are setting cookie one.
Giving that kookier value of electron.
That's our cookie object, so next, let's set it on the default session session, not cookies, Dot
said, passing in this new cookie object and then exactly the same as the get method.
This returns a promise as soon as the cookie is created.
So resolved that promise with then no arguments in this callback as we're simply creating a cookie and
not retrieving anything in which our logout cookie one set, at which point we can immediately get all
the cookies again to see that new addition using this kit cookies helper function or copy this and comment
out the date finish load listener paste.
Note that as before, should anything go wrong when creating the cookie, we can get those errors with
a catch function on the return promise.
Exactly the same as this.
I'll just leave that for now though.
Save.
Check the terminal and there we still have all the cookies set by GitHub, dotcom, but also our own
cookie, one very importantly noticed that our cookie has the session property set to true, whereas
all the other cookies have false with an expiration date.
Instead, what this means is that without an expiration date, our cookie is only valid for the duration
of this particular session, not the session object, although that word exists, but this very instance
of the app being launched, meaning it's not persisted in the session object as it does not have an
expiry check.
This are comment out the set function.
Logging out all the cookies, once the content is loaded again, save.
And gone, we're back to only having the cookies created with an expiration, so to make a custom cookie
persist between applications, then we can simply give it an expiration in the future or use this expiration
time from the October cookie as being a simple Unix timestamp, revert back to our set function as before,
this time sitting on the cookie object expiration date, that time stamp I copied.
You can also make this infinite to have infinitely persistent cookies save again.
We see Cookie one logged out as before, but now containing an expiration date and no longer being marked
as a session cookie check, this persisted by quitting the app manually.
This always being best practice for ensuring session data has persisted.
Comment out the said function reading on did finish load save, which launches the app, and in the
terminal cookie one is still being logged, meaning it's now persisted until this date here, at which
point it will be removed automatically.
Next, let's see how to read specific cookies.
I'll clear this and if you recall I mentioned earlier that passing cookies don't get an empty object
will return all cookies.
So to read a specific cookie only, let's say our cookie one, we can pass a number of filters to this
object.
For example, name cookie, one save.
And we only get that one cookie with the matching name.
We can also filter by domain, Eurail, etc..
Finally, let's remove Cookie.
One comment out our did finish load Lessner.
Calling session dot cookies dot remove first argument being the euro, my domain dot com and second
being the name cookie one, this being as cookies with the same name can exist under different domains.
Or you URLs as with all cookie functions, remove returns, a promise so resolved with then meaning
the cookie was successfully removed in which we can again get cookie one or at least check if it exists.
Save switch back to the terminal and nothing.
We get an empty array as cookie one no longer exists.
That's the basics of using cookies in electron.
There's also an event for detecting cookie changes, but for the most part, getting, setting and removing
should suffice.